<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{width:200px;height:200px;background:red;display: none;}
    </style>
</head>
<body>
    <input type="button" value="显示或隐藏" id="btn">
    <div class="box"></div>
</body>
<script>

    var btn = document.querySelector("#btn");
    var box = document.querySelector(".box");

    var type = 0;

    btn.onclick = function(eve){

        var e = eve || window.event;

        // 正常
        // e.stopPropagation();
        // e.cancelBubble = true;
        stopBubble(e);
        


        if(type === 0){
            box.style.display = "block";
            type = 1;
        }else{
            box.style.display = "none";
            type = 0;
        }

        console.log(1)
    }
    
    document.onclick = function(){
        box.style.display = "none";
        type = 0;
        console.log(2)
    }



    function stopBubble(e){
        if(e.stopPropagation){
            e.stopPropagation();
        }else{
            e.cancelBubble = true;
        }
    }
    
</script>
</html>